/**
 * MockDataAdapter - Provides data access through the Mock API Service
 */

import { MockApiService } from './MockApiService';
import { LoadOptions, SortInfo } from '../services';

/**
 * MockDataAdapter class - Adapts DataSource to use mock data
 */
export class MockDataAdapter {
  private static _instance: MockDataAdapter;
  private _mockApiService: MockApiService;
  
  private constructor() {
    this._mockApiService = MockApiService.getInstance();
  }
  
  /**
   * Get the singleton instance of the MockDataAdapter
   */
  public static getInstance(): MockDataAdapter {
    if (!MockDataAdapter._instance) {
      MockDataAdapter._instance = new MockDataAdapter();
    }
    return MockDataAdapter._instance;
  }
  
  /**
   * Load data for a specific object type with options
   * @param objectTypeName The business object type name
   * @param options LoadOptions for filtering, sorting, etc.
   */
  public loadData(objectTypeName: string, options: LoadOptions = {}): Promise<any> {
    return Promise.resolve(
      this._mockApiService.getObjects(
        objectTypeName,
        options.filter,
        options.sort,
        options.skip,
        options.take,
        options.expand,
        options.select
      )
    );
  }
  
  /**
   * Get a single object by key
   * @param objectTypeName The business object type name
   * @param key The key value
   * @param options LoadOptions for expansion and selection
   */
  public getObjectByKey(objectTypeName: string, key: any, options: LoadOptions = {}): Promise<any> {
    return Promise.resolve(
      this._mockApiService.getObjectByKey(
        objectTypeName,
        key,
        options.expand,
        options.select
      )
    );
  }
  
  /**
   * Create a new object
   * @param objectTypeName The business object type name
   * @param data The data for the new object
   */
  public createObject(objectTypeName: string, data: any): Promise<any> {
    return Promise.resolve(
      this._mockApiService.createObject(objectTypeName, data)
    );
  }
  
  /**
   * Update an existing object
   * @param objectTypeName The business object type name
   * @param key The key value
   * @param data The updated data
   */
  public updateObject(objectTypeName: string, key: any, data: any): Promise<any> {
    return Promise.resolve(
      this._mockApiService.updateObject(objectTypeName, key, data)
    );
  }
  
  /**
   * Delete an object
   * @param objectTypeName The business object type name
   * @param key The key value
   */
  public deleteObject(objectTypeName: string, key: any): Promise<void> {
    return Promise.resolve(
      this._mockApiService.deleteObject(objectTypeName, key)
    );
  }
  
  /**
   * Get the application model
   */
  public getApplicationModel(): Promise<any> {
    return Promise.resolve(
      this._mockApiService.getApplicationModel()
    );
  }
}
